/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.addressbook.utils;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 *
 * @author Michal Ďuriš
 */
public class DateCreator {

    private DateCreator() {
    }

    /**
     *
     * @param day       the value of day
     * @param month     the value of month
     * @param year      the value of year
     * @param errorList
     *
     * @return
     */
    public static LocalDate validateAndCreateDate(String day, String month,
                                                  String year,
                                                  List<String> errorList) {

        int dayValue = 0;
        int monthValue = 0;
        int yearValue = 0;

        LocalDate date = null;

        try {
            dayValue = parseInt(day);
        } catch (NumberFormatException ex) {
            errorList.add("day");
        }
        try {
            monthValue = parseInt(month);
        } catch (NumberFormatException ex) {
            errorList.add("month");
        }
        try {
            yearValue = parseInt(year);
        } catch (NumberFormatException ex) {
            errorList.add("year");
        }

        if (errorList.isEmpty()) {
            try {
                date = LocalDate.of(yearValue, monthValue, dayValue);

            } catch (DateTimeException exception) {
                errorList.add("date");
            }
        }

        return date;
    }

}
